package Object.reflect;

import java.lang.reflect.Constructor;
import java.util.Arrays;

/**
 * @author haomin
 * @date 2022/11/09 15:51
 **/
public class ReflectTest {
    public static void main(String[] args) throws Exception {
//        Person per1 = new Person();
//        Person per2 = new Student();
//
//        Class c1 = per1.getClass();
//        Class c11 = Class.forName("Object.reflect.Person");
//        Class c111 = new Person().getClass();
//        System.out.println(c1 == c11);
//        System.out.println(c11 == c111);
//
//        Class c2 = per2.getClass();
//        System.out.println(c1);
//        System.out.println(c2);

//        Class<Student> cla = Student.class;
//        Student student = cla.newInstance();
//        System.out.println(student);

        Class<Student> st = Student.class;
        Constructor<?>[] constructors = st.getConstructors();
        System.out.println(Arrays.toString(constructors));
        // 无参构造
        Constructor<Student> constructor = st.getConstructor();
        System.out.println(constructor);
        // 不能访问default 权限
//        Constructor<Student> constructor1 = st.getConstructor(String.class);
//        System.out.println(constructor1);

        // 可以访问如何权限
        Constructor<?>[] declaredConstructors = st.getDeclaredConstructors();
        System.out.println(Arrays.toString(declaredConstructors));

//        Student student = new Student();
        Constructor<Student> declaredConstructor = st.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        Student student = declaredConstructor.newInstance();

        Constructor<Student> dl1 = st.getDeclaredConstructor(Student.class);
        dl1.setAccessible(true);
        Student student1 = dl1.newInstance("张三");
        // 和 Student s = new Studend("张三") 一致，只是用反射的方式
    }
}